package com.kyle.pagingdemo.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.doOnPreDraw
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDirections
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.material.transition.Hold
import com.google.android.material.transition.MaterialFadeThrough
import com.google.android.material.transition.MaterialSharedAxis
import com.kyle.pagingdemo.R
import com.kyle.pagingdemo.viewmodel.PagingViewModel
import com.kyle.pagingdemo.databinding.FragmentHomeBinding
import com.kyle.pagingdemo.ui.adapter.PostAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : Fragment() {

    private val pagingViewModel: PagingViewModel by activityViewModels()

    private lateinit var binding: FragmentHomeBinding
    private lateinit var postAdapter: PostAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("TAG", "onCreate ${pagingViewModel.postFlow}")
//        exitTransition = Hold()
        enterTransition = MaterialFadeThrough()
        exitTransition = MaterialFadeThrough()
//        enterTransition = MaterialSharedAxis(
//            MaterialSharedAxis.Z, false)
//        exitTransition = MaterialSharedAxis(
//            MaterialSharedAxis.Z, true)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        Log.d("TAG", "onCreateView: ")
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        postAdapter = PostAdapter { view: View, position: Int ->
            navigateHandler(
                view,
                getString(R.string.container_card_item, position),
                HomeFragmentDirections.actionHomeFragmentToDetailFragment(position)
            )
        }
        binding.recyclerview.apply {
            layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = postAdapter

        }
        with(binding.refresh) {
            with(postAdapter) {
                setOnRefreshListener {
                    refresh()
                    binding.recyclerview.scrollToPosition(0)
                }
                lifecycleScope.launch {
                    pagingViewModel.postFlow.collectLatest {
                        Log.d("TAG", "PagingData: $it")
                        submitData(it)
                    }
                }
                lifecycleScope.launch {
                    loadStateFlow.collectLatest {
                        finishRefresh(it.prepend.endOfPaginationReached)
                        Log.d("TAG", "LoadState: ${it.prepend}")
                    }
                }
            }

        }
        return binding.root
    }

    private fun navigateHandler(view: View, transitionName: String, navDirections: NavDirections) {
        val extras = FragmentNavigatorExtras(
            view to transitionName
        )
        findNavController().navigate(navDirections, extras)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d("TAG", "onCreateView")
        postponeEnterTransition()
        view.doOnPreDraw { startPostponedEnterTransition() }

    }
}